package zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

public class ZKConnection {

    public static final String CONNECTSTRING = "node101:2181,node102:2181,node103:2181";
    public static final int sessionTimeout = 10000;

    public static void main(String[] args) throws Exception {

        ZooKeeper zooKeeper = new ZooKeeper(
                CONNECTSTRING,
                sessionTimeout,
                event -> {
                    System.out.println("事件类型："+event.getType());
                    System.out.println("事件发生的路径："+event.getPath());
                    System.out.println("事件状态："+event.getState());
                }
        );

        List<String> nodesByRoot = zooKeeper.getChildren("/",true);
        System.out.println(nodesByRoot);

        zooKeeper.close();
    }

}
